package com.wzh.yebServe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.yebServe.mapper.MenuRoleMapper;
import com.wzh.yebServe.pojo.MenuRole;
import com.wzh.yebServe.pojo.ResultBean;
import com.wzh.yebServe.service.IMenuRoleService;
import com.wzh.yebServe.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wzh
 * @create 2022/11/17 12:55
 * @Description:
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<Integer> getMidByRid(Integer rid) {
        List<MenuRole> rid1 = query().eq("rid", rid).list();
        List<Integer> collect = rid1.stream().map(MenuRole::getMid).collect(Collectors.toList());
        return collect;
    }

    @Override
    @Transactional
    public ResultBean updateMenuRole(Integer rid, Integer[] mids) {
        LambdaQueryWrapper<MenuRole> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(MenuRole::getRid,rid);
//        删除角色下的所有菜单
        boolean remove = remove(lambdaQueryWrapper);
        if (null==mids||0==mids.length){
            return ResultBean.error("更新失败");
        }
        menuRoleMapper.insertRecord(rid,mids);
        return ResultBean.success("更新成功");
    }
}
